//
//  YSPresentationController.m
//  YouSheng
//
//  Created by aaa on 16/8/24.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//
#import "YSPresentationController.h"
@interface YSPresentationController()
@property (nonatomic, strong) UIButton *coverButton;

@property (nonatomic, assign) BOOL isPresented;

@end
@implementation YSPresentationController

- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController {
    if (self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController]) {
        
    }
    return self;
}

- (void)containerViewWillLayoutSubviews {
    self.presentedView.frame = CGRectMake(160, 56, 200, 220);
}


/// 自定义转场即将展示时调用
-(void)presentationTransitionWillBegin {
    [super presentationTransitionWillBegin];
    _coverButton = [[UIButton alloc] init];
    _coverButton.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.5];
    _coverButton.frame = self.containerView.bounds;
    [_coverButton addTarget:self action:@selector(coverBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:_coverButton];
}

- (void)coverBtnClick:(id)sender {
    // 关闭modal控制器
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
//#pragma mark - UIViewControllerTransitioningDelegate
///// 该代理方法用于返回负责转场的控制器对象
//- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
//    return [[YSPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
//}
//
///// 该代理方法用于告诉系统谁来负责控制器如何弹出
//- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
//    _isPresented = YES;
//    return self;
//}
//
///// 该代理方法用于告诉系统谁来负责控制器如何消失
//- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
//{
//    _isPresented = NO;
//    return self;
//}
//
///// 用于返回动画的时长, 默认用不上
//- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
//    return 1.0;
//}
//
//// 系统调用该方法时会传递一个transitionContext参数, 该参数中包含了我们所有需要的值
//- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
//    if (_isPresented) {
//        [self animatePresentedController:transitionContext];
//    } else{
//        [self animateDismissedController:transitionContext];
//    }
//}
//
///// 弹出动画
//- (void)animatePresentedController:(id<UIViewControllerContextTransitioning>)transitionContext {
//    NSTimeInterval duration = [self transitionDuration:transitionContext];
//    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
//    [[transitionContext containerView] addSubview:toView];
//    toView.transform = CGAffineTransformMakeScale(1.0, 0.0);
//    toView.layer.anchorPoint = CGPointMake(0.5, 0);
//    [UIView animateWithDuration:duration animations:^{
//        toView.transform = CGAffineTransformIdentity;
//    } completion:^(BOOL finished) {
//        [transitionContext completeTransition:YES];
//    }];
//    
//}
//
///// 隐藏动画
//- (void)animateDismissedController:(id<UIViewControllerContextTransitioning>)transitionContext {
//    NSTimeInterval duration = [self transitionDuration:transitionContext];
//    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//    [UIView animateWithDuration:duration animations:^{
//        fromView.transform = CGAffineTransformMakeScale(1.0, 0.0001);
//    } completion:^(BOOL finished) {
//        [transitionContext completeTransition:YES];
//    }];
//}
@end
